package com.example.list;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import static java.security.AccessController.getContext;

public class DynamicViewActivity extends AppCompatActivity {
    private RelativeLayout rlDynamic;
    private EditText etDynamic;
    private TextView tvDynamic;
    private EditText etDynamicLl;
    private TextView tvDynamicLl;
    private RelativeLayout Rl;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic_view);

        rlDynamic = (RelativeLayout) findViewById(R.id.rl_dynamic);
        etDynamic = (EditText) findViewById(R.id.et_dynamic);
        tvDynamic = (TextView) findViewById(R.id.tv_dynamic);


        etDynamicLl = (EditText) findViewById(R.id.et_dynamic_ll);
        tvDynamicLl = (TextView) findViewById(R.id.tv_dynamic_ll);

        m1();
        m2();


        Rl = (RelativeLayout) findViewById(R.id.Rl);
        tv = (TextView) findViewById(R.id.tv);

        Rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv.getText().equals("tv")) {
                    tv.setEnabled(false);
                    tv.setText("false");
                } else {
                    tv.setEnabled(true);
                }
            }
        });

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Rl.setBackgroundColor(Color.BLACK);
            }
        });

    }

    public void m2() {

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
        lp.gravity = Gravity.CENTER;
        lp.rightMargin = 10;
        etDynamicLl.setLayoutParams(lp);

        lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
        lp.gravity = Gravity.CENTER;
        lp.leftMargin = 10;
        tvDynamicLl.setLayoutParams(lp);
    }

    public void m1() {
        RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        p1.addRule(RelativeLayout.CENTER_IN_PARENT);
        p1.rightMargin = 200;

//        设置图片的位置
        etDynamic.setLayoutParams(p1);

        RelativeLayout.LayoutParams p2 = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        p2.addRule(RelativeLayout.CENTER_IN_PARENT);
//        p2.addRule(RelativeLayout.CENTER_HORIZONTAL|RelativeLayout.CENTER_VERTICAL);
        p2.leftMargin = 200;

        //设置图片的位置
        tvDynamic.setLayoutParams(p2);
    }

    public void getTask(View view) {

        Toast.makeText(this, ""+getRunningActivityName2(), Toast.LENGTH_SHORT).show();
    }


    private String getRunningActivityName2(){
        ActivityManager activityManager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        String runningActivity= null;

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
//           ComponentName className = activityManager.getRunningTasks(1).get(0).realActivity;
//            if (className!=null) {
//                runningActivity=className.getClassName();
//            }
//        }
        // 获取activity任务栈
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.RunningTaskInfo info = manager.getRunningTasks(1).get(0);
        List<ActivityManager.RunningTaskInfo> list= manager.getRunningTasks(1);
        String packageName = info.topActivity.getPackageName();
        String topclassName = info.topActivity.getClassName();
        String baseclassname = info.baseActivity.getClassName();
        int acitivitynum = info.numActivities;

        return runningActivity;
    }
}